package service4admin

import (
	"Gin-Gorm-boke/boke_admin/models4admin"
	"Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strconv"
)

func GetMenuList(c *gin.Context) {
	var menuList []models4admin.Menu
	models.Db.Find(&menuList)
	response.ResponseOK(c, menuList)
	return
}

func SaveMenu(c *gin.Context) {
	var menu models4admin.Menu
	if err := c.ShouldBindJSON(&menu); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	models.Db.Create(&menu)
	response.ResponseOK(c, nil)
	return
}

func UpdateMenu(c *gin.Context) {
	var menu models4admin.Menu
	if err := c.ShouldBindJSON(&menu); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	if menu.ParentId.Int64 == int64(menu.Id) {
		c.JSON(500, gin.H{
			"code": 500,
			"msg":  "父级菜单不能为自身",
		})
		return
	}
	models.Db.Model(new(models4admin.Menu)).Where("id=?", menu.Id).Updates(&menu)
	response.ResponseOK(c, nil)
	return
}

func GetMenuById(c *gin.Context) {
	id := c.Param("id")
	var menu models4admin.Menu
	models.Db.Where("id = ?", id).First(&menu)
	response.ResponseOK(c, menu)
	return
}

func DelMenuById(c *gin.Context) {
	id := c.Param("id")
	models.Db.Where("id = ?", id).Update("del_flag", 1)
	models.Db.Where("id=?", id).Delete(new(models4admin.Menu))
	var count int64
	models.Db.Model(new(models4admin.Menu)).Where("parent_id=?", id).Count(&count)
	if count > 0 {
		c.JSON(500, gin.H{
			"code": 500,
			"msg":  "删除失败，请先删除子菜单",
		})
		return
	}
	response.ResponseOK(c, nil)
	return
}
func GetMenuTree(c *gin.Context) {
	var menuVoList []models4admin.MenuVo
	menuVoList = GetMenuWithChildren(0)
	response.ResponseOK(c, menuVoList)
	return
}

func GetMenuTreeById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var menuIds []int
	models.Db.Model(new(models4admin.RoleMenu)).Select("menu_id").Where("role_id=?", id).Find(&menuIds)
	var menuVoList []models4admin.MenuVo
	for _, v := range menuIds {
		var menuVo models4admin.MenuVo
		var menu models4admin.Menu
		models.Db.Model(new(models4admin.Menu)).Where("id=?", v).Find(&menu)
		copier.Copy(&menuVo, &menu)
		menuVo.Children = GetMenuWithChildren(int(menu.Id))
		menuVoList = append(menuVoList, menuVo)

	}
	response.ResponseOK(c, menuVoList)
	return
}

func GetRouters(c *gin.Context) {
	userId, _ := c.Get("userId")

	var roleId int
	models.Db.Model(new(models4admin.UserRole)).Select("role_id").Where("user_id=?", userId).Find(&roleId)
	if roleId == 1 {
		var routerList []models4admin.Router
		var menuList []models4admin.Menu
		models.Db.Model(new(models4admin.Menu)).Where("parent_id = ?", 0).Find(&menuList)
		copier.Copy(&routerList, &menuList)
		for i, v := range routerList {
			var routerChildrenList []models4admin.Router
			var menuChildrenList []models4admin.Menu
			models.Db.Model(new(models4admin.Menu)).Where("parent_id=?", v.Id).Find(&menuChildrenList)
			copier.Copy(&routerChildrenList, &menuChildrenList)
			routerList[i].IdStr = strconv.Itoa(v.Id)
			routerList[i].ParentIdStr = strconv.Itoa(int(v.ParentId.Int64))
			routerList[i].Children = routerChildrenList
		}

		response.ResponseOK(c, gin.H{
			"menus": routerList,
		})
		return
	}
	response.ResponseOK(c, nil)
	return

}

func GetMenuWithChildren(parentId int) []models4admin.MenuVo {
	var menuList []models4admin.Menu
	var menuVoList []models4admin.MenuVo
	models.Db.Model(new(models4admin.Menu)).Where("parent_id=?", parentId).Find(&menuList)
	copier.Copy(&menuVoList, &menuList)
	for i, v := range menuVoList {
		menuVoList[i].Children = GetMenuWithChildren(v.Id)
	}
	return menuVoList

}

func GetRouterByParentId(parentId int) []models4admin.Router {
	var menuList []models4admin.Menu
	var routers []models4admin.Router
	models.Db.Model(new(models4admin.Menu)).Where("parent_id=?", parentId).Find(&menuList)
	copier.Copy(&routers, &menuList)
	for i, v := range routers {
		routers[i].IdStr = strconv.Itoa(v.Id)
		routers[i].Children = GetRouterByParentId(v.Id)
	}
	return routers

}
